package com.buchong.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.UUID;


@RestController
@RequestMapping("/pay")
public class PayController {
    @RequestMapping("/alipay")
    public void show(HttpServletRequest request, HttpServletResponse response,@RequestParam("dingdanhao") String dingdanhao,@RequestParam String zongjine,@RequestParam  String id)
            throws IOException {
        AlipayClient alipayClient = new
                DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                "2016102200735642",
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC/sFGYxP/C8oRog9/qdSd8Il4SerGF0S/mC838zhTTRCm5tkKjvnzQyGqx4mB454Nu6v9JS5kiHaacLla16pEtfY68WSW3LGAb0G3LjTEkwZbpCHtBo/a+FOn/2zzG+vmWcDv7HtsSOmDnUnYP8jeSOMf4CYP/BMFzJiY21qRF+crqcgbeTX8KbO6aw+LjwosB+jD6c8/jqJ/o9L+dG0xSpMeQZ2H84UxnDxMSKSHJEJxvQ8vTDkiBXPcF57NUvCHzGDSwCsQyZo6VuZrtOPqQxBQl4bvmtL8itXvkBBoYWOYO4g3p5sTsr371UfLHqil0iSGWieun4ljPs0PFizJRAgMBAAECggEBALTI5NvxB8pjkU9E+LyWC0w+2XwSLTUoykTrYzWHYwqxYazd4VGvSOZb7oygFR5SMpo8ldIZs24Ciu0How3lEVDW9vRaG4D7IBV7aOGt4uhokBxtvFl2+zHgZaTBrzt23kfsyBUZz8EMP/JKykBVz+VfGfGrf8rXQF+KXkYf/0RiJQHe46XBeC9fjs2B+ZW9JYTv+g9aFoShYBFIrrVk2Q5Zz8+qGKtw1i2EsOnEaw4rA/WY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rahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB",
                "RSA2" );
        //上面这个方法是不是很眼熟？就是刚才配置完公钥后生成的那段，你可以直接复制然后加上你的私钥就好
        UUID uuid = UUID.randomUUID();
        String out_trade_no = dingdanhao+uuid;//订单号
        out_trade_no = URLDecoder.decode(out_trade_no,"UTF-8");
        String total_amount = zongjine;//总金额
        total_amount = URLDecoder.decode(total_amount,"UTF-8");
        String subject = "网上商城自营图书   ";//商品名称
        subject = URLDecoder.decode(subject,"UTF-8");
        String body = " ";//商品描述
        body = URLDecoder.decode(body,"UTF-8");

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request
        alipayRequest.setReturnUrl("http://localhost:10088/order/success?id="+id);
        alipayRequest.setBizContent("{" +
                "    \"out_trade_no\":\""+ out_trade_no +"\"," +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
                "    \"total_amount\":"+ total_amount +"," +
                "    \"subject\":\""+ subject +"\"," +
                "    \"body\":\""+ body +"\"" +
                "    }"+
                "  }");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println(form);//直接将完整的表单html输出到页面
        response.getWriter().close();

    }


}
